package com.lagou.listener;

import com.lagou.entity.Order;
import com.lagou.repository.OrderRepository;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@RocketMQMessageListener(topic = "tp_delay_pay", consumerGroup = "pay_consumer")
public class PayListener implements RocketMQListener<String> {
    @Autowired
    private OrderRepository orderRepository;
    @Resource
    private RedisTemplate<String, Integer> redisTemplate;

    @Override
    public void onMessage(String orderId) {
        Order order = orderRepository.findById(orderId).get();
        if (!order.getOrderStatus().equals("已支付")) {

            //订单设置为超时未支付
            order.setOrderStatus("已超时");
            orderRepository.save(order);

            //恢复库存
            redisTemplate.opsForValue().increment("goods_count", 1);
            System.out.println("订单已取消。。");
            return;
        }

        System.out.println(orderId + "订单已支付。。");

    }
}
